package com.example.waterfall.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.example.waterfall.model.BitmapInfo;

import android.util.Log;
import android.util.Xml;

/**
 * 解析XML工具类
 */
public class ParseService {

	/**
	 * 解析XML转换成对象
	 * 
	 * @param is
	 *            输入流
	 * @param clazz
	 *            对象Class
	 * @param fields
	 *            字段集合一一对应节点集合
	 * @param elements
	 *            节点集合一一对应字段集合
	 * @param itemElement
	 *            每一项的节点标签
	 * @return
	 */
	public static List<Object> parseXml(InputStream is, Class<?> clazz,
			List<String> fields, List<String> elements, String itemElement) {
		Log.v("rss", "开始解析XML.");
		List<Object> list = new ArrayList<Object>();
		try {
			XmlPullParser xmlPullParser = Xml.newPullParser();
			xmlPullParser.setInput(is, "UTF-8");
			int event = xmlPullParser.getEventType();

			Object obj = null;

			while (event != XmlPullParser.END_DOCUMENT) {
				switch (event) {
				case XmlPullParser.START_TAG:
					if (itemElement.equals(xmlPullParser.getName())) {
						obj = clazz.newInstance();
					}
					if (obj != null
							&& elements.contains(xmlPullParser.getName())) {
						String propertyName = fields.get(elements
								.indexOf(xmlPullParser.getName()));

						String value = "";
						if ("Works".equals(xmlPullParser.getName())) {
							value = xmlPullParser.getAttributeValue(0);
						}
						value = xmlPullParser.nextText();
						;

						setFieldValue(obj, propertyName, value);
					}
					break;
				case XmlPullParser.END_TAG:
					if (itemElement.equals(xmlPullParser.getName())) {
						list.add(obj);
						obj = null;
					}
					break;
				}
				event = xmlPullParser.next();
			}
		} catch (Exception e) {
			Log.e("rss", "解析XML异常：" + e.getMessage());
			throw new RuntimeException("解析XML异常：" + e.getMessage());
		}
		return list;
	}

	/**
	 * 
	 * @param xml
	 *            解析字符串
	 * @param clazz
	 *            实体
	 * @param fields
	 *            实体字段集合
	 * @param elements
	 *            xml内标签集合
	 * @param itemElement
	 *            开始标签体
	 * @return
	 */
	public static List<Object> parseXml(String xml, Class<?> clazz,
			List<String> fields, List<String> elements, String itemElement) {
		String str = xml;
		InputStream inputStream = new ByteArrayInputStream(str.getBytes());
		return parseXml(inputStream, clazz, fields, elements, itemElement);
	}

	/**
	 * 设置字段值
	 * 
	 * @param propertyName
	 *            字段名
	 * @param obj
	 *            实例对象
	 * @param value
	 *            新的字段值
	 * @return
	 */
	public static void setFieldValue(Object obj, String propertyName,
			Object value) {
		try {
			Field field = obj.getClass().getDeclaredField(propertyName);
			field.setAccessible(true);
			field.set(obj, value);
		} catch (Exception ex) {
			throw new RuntimeException();
		}
	}

	
	/**
	 * 解析xml,获取数据集合
	 * 
	 * @param xml 现指定为Bitmap相关的xmL数据
	 * @return  Bitmapinfo的集合
	 */
	public static List<BitmapInfo> parseXml(String xml) {
		InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
		return parseXml(inputStream);
	}
	
	/**
	 * 解析xml,获取数据集合
	 * 
	 * @param xml 现指定为Bitmap相关的xmL数据
	 * @return  Bitmapinfo的集合
	 */
	public static List<BitmapInfo> parseXml(InputStream inputStream) {
		
		List<BitmapInfo> list = null;
		BitmapInfo info = null;
		XmlPullParser xmlPullParser = Xml.newPullParser();
		try {
			xmlPullParser.setInput(inputStream, "UTF-8");
			int eventType = xmlPullParser.getEventType();
			
			while (eventType != XmlPullParser.END_DOCUMENT) {
				String nodeName = xmlPullParser.getName();
				switch (eventType) {
				// 文档开始
				case XmlPullParser.START_DOCUMENT:
					list = new ArrayList<BitmapInfo>();
					break;
					// 开始节点
				case XmlPullParser.START_TAG:
					// 判断如果其实节点为student
					if ("Works".equals(nodeName)) {
						// 实例化BitmapInfo对象
						info = new BitmapInfo();
						// 设置Id属性
						info.setId(xmlPullParser.getAttributeValue(0));
					} else if ("icon".equals(nodeName)) {
						// 设置picture
						info.setIcon(xmlPullParser.nextText());
					} else if ("picture".equals(nodeName)) {
						// 设置title
						info.setPicture(xmlPullParser.nextText());
					} else if ("title".equals(nodeName)) {
						// 设置address
						info.setTitle(xmlPullParser.nextText());
					} else if ("address".equals(nodeName)) {
						// 设置address
						info.setLcation(xmlPullParser.nextText());
					}
					//其他
					break;
					// 结束节点
				case XmlPullParser.END_TAG:
					if ("Works".equals(nodeName)) {
						list.add(info);
						info = null;
					}
					break;
				default:
					break;
				}
				eventType = xmlPullParser.next();
			}
		} catch (NumberFormatException e) {
			
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}
	

}
